<script setup lang='ts'>
import { reactive, ref, onMounted, watch } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { CircleCloseFilled, Plus } from '@element-plus/icons-vue'
import type { IAddRoles,IRoles } from '@/types/roles'
import type { IDept } from '@/types/dept'
import http from '../../api/http'
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<IAddRoles>({
      roleId:1,
    roleName:'',
    deptId:1,
    type: 1,
    remark: '',
    lastTime:new Date(),
})




//表单验证
const rules = reactive<FormRules<IAddRoles>>({
    roleName: [
        { required: true, message: '请输入角色姓名!', trigger: 'blur' },
        { min: 2, max: 10, message: '长度在2-10个字符之间!', trigger: 'blur' },
    ],
    deptId: [
        { required: true, message: '请选择部门!', trigger: 'change', }
    ]
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {

        if (valid) {
            //axios 提交后台

            http.put('Roles', ruleForm).then(res => {
                if (res.code == '1') {
                    ElMessage({
                        message: res.msg,
                        type: 'success',
                    })
                    dialogVisible.value = false
                    emit('refresh')
                }
                else {
                    ElMessage({
                        message: res.msg,
                        type: 'error',
                    })
                }

            }).catch(err => {
                console.log('err: ', err);
            })


            //关闭弹窗 并刷新列表页面数据
            dialogVisible.value = false
            emit('refresh')
        } else {
            console.log('error submit!', fields)
        }
    })
}

//#region  父子组件之间双向绑定数据 数据传递
// 父子组件之间双向绑定数据 数据传递
const dialogVisible = defineModel<boolean>({required:true})
const emit = defineEmits<{ (e: 'refresh'): void }>()
const props = defineProps<{ formData: IRoles }>()
//监听key 的改变 并查询数据
watch(() => props.formData, (value: IRoles) => {
    //切断数据关联
    let data: IRoles = JSON.parse(JSON.stringify(value))

    //form 赋值
    ruleForm.roleId = data.roleId;
    ruleForm.roleName = data.roleName;
    ruleForm.remark = data.remark;;
    ruleForm.type = data.type;
    ruleForm.deptId = data.deptId;
    ruleForm.lastTime = data.lastTime;
    //ruleForm = data
    //console.log('ruleForm: ', ruleForm)

})

//#endregion

const dept: IDept[] = reactive([])

onMounted(() => {
    //获取用户部门下拉列表
    http.get<IDept[]>('Dept')
        .then(res => {
            dept.push(...res.data)
        })
        .catch(err => {
            console.log('err: ', err);
        })

})

</script>

<template>

    <el-dialog v-model="dialogVisible" :show-close="false" width="500" center>
        <template #header="{ close, titleId, titleClass }">
            <div class="my-header">
                <h4 :id="titleId" :class="titleClass"></h4>
                <el-button type="danger" @click="close">
                    <el-icon class="el-icon--left">
                        <CircleCloseFilled />
                    </el-icon>
                    关闭
                </el-button>
            </div>
        </template>

       
            <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
             <el-form-item label="角色名称" prop="roleName">
                <el-input v-model="ruleForm.roleName" />
            </el-form-item>
            <el-form-item label="部门名称" prop="deptId">
                <el-select v-model="ruleForm.deptId" placeholder="请选择部门">
                    <el-option v-for="(item, index) in dept" :key="index" :label="item.deptName" :value="item.deptId">
                        {{ item.deptName }}
                    </el-option>
                </el-select>
            </el-form-item>
           
           
            <el-form-item label="部门描述" prop="remark">
                <el-input v-model="ruleForm.remark" />
            </el-form-item>
        </el-form>


        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    确定
                </el-button>
            </div>
        </template>
    </el-dialog>

</template>


<style scoped>
.my-header {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    gap: 16px;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 128px;
    height: 128px;
    line-height: 128px;
    text-align: center;
}

.avatar {
    width: 128px;
    height: 128px;
    display: block;
}
</style>